<template>
  <view>
    <up-tabs
      :list="[{ name: '党纪知识' }, { name: '典型案例' }, { name: '廉政漫画' }]"
      @click="
        (item, index) => {
          news.current = index;
          news.list = [];
          news.queryParams = {
            pageNum: 1,
            pageSize: 10,
            type: index + 1,
          };
          initNews();
        }
      "
    >
    </up-tabs>
    <view class="news-list bg-white px-5.25 pt-1 pb-3 box-border min-h-[100vh]">
      <template v-if="news.list.length">
        <view
          class=""
          v-for="(item, index) in news.list"
          :key="index"
          @click="scanNewsDetail(item)"
        >
          <newsListItem
            :newsTitle="item.title"
            :newsTime="
              item.createTime ? dayjs(item.createTime).format('YYYY-MM-DD') : ''
            "
            :newsImg="imgPrefix + item.imgUrl"
          ></newsListItem>
        </view>
      </template>
      <up-empty
        v-else
        mode="history"
        text="暂无数据"
        icon="http://cdn.uviewui.com/uview/empty/history.png"
      >
      </up-empty>
      <up-toast ref="uToastRef"></up-toast>
    </view>
  </view>
</template>
<script setup lang="ts">
import newsListItem from "@/components/newsListItem/newsListItem.vue";
import { integritylistPage } from "@/api/news/index";
import { imgPrefix } from "@/config/app";
import dayjs from "dayjs";
const uToastRef = <any>ref(null);
const data = reactive({
  news: {
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      type: 1,
    },
    current: 0,
    list: <any>[],
    total: 0,
    request: [integritylistPage],
  },
});
const { news } = toRefs(data);
// const status = ref("loadmore");
onLoad((options) => {
  initNews();
});
const initNews = () => {
  uni.showLoading({
    title: "加载中",
    mask: true,
  });
  news.value.request[0](news.value.queryParams)
    .then((res: any) => {
      // const { data } = res;
      // news.value.list = data.records ?? [];
      uni.hideLoading();
      const { data } = res;
      data.records = data.records ?? [];
      news.value.list = [...news.value.list, ...data.records];
      news.value.total = data.total;
      uni.stopPullDownRefresh();
    })
    .catch((err: any) => {
      news.value.list = [];
      uToastRef.value.show({
        type: "error",
        message: err,
      });
    });
};
// 查看新闻详情
const scanNewsDetail = (item: any) => {
  uni.navigateTo({
    // url: `/subPages/crowd/news/detail?newsId=${item.id}&newsType=${news.value.current}`,
    url: `/subPages/crowd/news/detail?newsId=${item.id}&newsType=4`,
  });
  // if (userInfoStore.token && userInfoStore.openId) {

  // } else {
  //   showModal.value = true;
  // }
};
onReachBottom(() => {
  let allTotal =
    news.value.queryParams.pageNum * news.value.queryParams.pageSize;
  if (allTotal < news.value.total) {
    // status.value = "loading";
    news.value.queryParams.pageNum++;
    initNews();
  } else {
    // status.value = "nomore";
    uToastRef.value.show({
      type: "info",
      message: "暂无更多数据",
    });
    return;
  }
});
</script>
